package com.zx.mes.hyl.base.collections;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

/**
 * 集合自定义排序
 *
 * @author huayunlong
 * @date 2018-7-19
 */
public class TestCollections {


    public static int[] array = new int[]{1, 2, 7, 4, 9, 10, 23, 44, 11, 12};

    @Test
    public void test() {
        Arrays.sort(array);
        for (int i:array) {
            System.out.println(i);
        }
    }

    @Test
    public void test2(){
        Person[] personArr = new Person[10];
        Person p1 = new Person();
        p1.setName("1");
        p1.setAge(1);

        Person p2 = new Person();
        p2.setName("2");
        p2.setAge(2);

        Person p3 = new Person();
        p3.setName("3");
        p3.setAge(3);

        Person p4 = new Person();
        p4.setName("4");
        p4.setAge(4);

        Person p5 = new Person();
        p5.setName("5");
        p5.setAge(5);

        personArr[0] = p5;
        personArr[1] = p3;
        personArr[2] = p2;
        personArr[3] = p1;
        personArr[4] = p4;


        AgetComparator comparator = new AgetComparator();

        Arrays.sort(personArr,comparator);
//        for (Person person:personArr) {
//            System.out.println(person.getName()+":"+person.getAge());
//        }
    }

    @Test
    public void test3(){
        ArrayList<Person> list = new ArrayList<>();
        Person p1 = new Person();
        p1.setName("1");
        p1.setAge(1);

        Person p2 = new Person();
        p2.setName("2");
        p2.setAge(2);

        Person p3 = new Person();
        p3.setName("3");
        p3.setAge(3);

        Person p4 = new Person();
        p4.setName("4");
        p4.setAge(4);

        Person p5 = new Person();
        p5.setName("5");
        p5.setAge(5);

        list.add(p5);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p1);


        AgetComparator comparator = new AgetComparator();
        Collections.sort(list,comparator);
        for (int i = 0; i <list.size() ; i++) {
            System.out.println(list.get(i).getName());
        }
    }
}
